/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.servlet.employee;

import com.entities.Claim;
import com.entities.ClaimStatus;
import com.entities.Employee;
import com.sessionbeans.ClaimFacadeLocal;
import com.sessionbeans.ClaimStatusFacadeLocal;
import com.sessionbeans.EmployeeFacadeLocal;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author msc
 */
public class AddingClaim extends HttpServlet {

    @EJB
    private EmployeeFacadeLocal employeeFacade;
    @EJB
    private ClaimStatusFacadeLocal claimStatusFacade;
    @EJB
    private ClaimFacadeLocal claimFacade;

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        addClaim(request, response);
    }

    protected void addClaim(HttpServletRequest request, HttpServletResponse response) {
        try {
            /* Create a new claim */
            Claim newClaim = new Claim();

            int maxClaimId = 1;
            for (Claim c : claimFacade.findAll()) {
                if (maxClaimId < c.getClaimId().intValue()) {
                    maxClaimId = c.getClaimId().intValue();
                }
            }

            /* Get the data of all parameters */
            String setDateString = request.getParameter("setDate");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date setDate = sdf.parse(setDateString);

            String header = request.getParameter("request");
            String provider = request.getParameter("provider");
            String address = request.getParameter("address");
            String phoneNumber = request.getParameter("phoneNumber");
            String email = request.getParameter("email");

            String expenseDateString = request.getParameter("expenseDate");
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
            Date expenseDate = sdf1.parse(expenseDateString);

            String medicine = "none";
            if (!request.getParameter("medicine").isEmpty()) {
                medicine = request.getParameter("medicine");
            }
            float costOfMedicine = 0.0f;
            if (!request.getParameter("costOfMedicine").isEmpty()) {
                costOfMedicine = Float.valueOf(request.getParameter("costOfMedicine")).floatValue();
            }

            String laboratoryTest = "none";
            if (!request.getParameter("laboratoryTest").isEmpty()) {
                laboratoryTest = request.getParameter("laboratoryTest");
            }
            float costOfLaboratoryTest = 0.0f;
            if (!request.getParameter("costOfLaboratoryTest").isEmpty()) {
                costOfLaboratoryTest = Float.valueOf(request.getParameter("costOfLaboratoryTest")).floatValue();
            }

            String surgery = "none";
            if (!request.getParameter("surgery").isEmpty()) {
                surgery = request.getParameter("surgery");
            }
            float costOfSurgery = 0.0f;
            if (!request.getParameter("costOfSurgery").isEmpty()) {
                costOfSurgery = Float.valueOf(request.getParameter("costOfSurgery")).floatValue();
            }

            String otherItems = "none";
            if (!request.getParameter("otherItems").isEmpty()) {
                otherItems = request.getParameter("otherItems");
            }
            float costOftherItems = 0.0f;
            if (!request.getParameter("costOftherItems").isEmpty()) {
                costOftherItems = Float.valueOf(request.getParameter("costOftherItems")).floatValue();
            }

            String note = "none";
            if (!request.getParameter("note").isEmpty()) {
                note = request.getParameter("note");
            }

            /* Set the data of all field */
            int claimStatusId = 1;
            int employeeId = 1;

            newClaim.setClaimId(maxClaimId + 1);
            newClaim.setDateOfClaim(setDate);
            newClaim.setHeader(header);
            newClaim.setServiceProvider(provider);
            newClaim.setProviderAddress(address);
            newClaim.setPhoneNumber(phoneNumber);
            newClaim.setEmail(email);

            newClaim.setDateOfExpense(expenseDate);
            newClaim.setMedicine(medicine);
            newClaim.setMedicineCost((long) costOfMedicine);
            newClaim.setLaboratoryTest(laboratoryTest);
            newClaim.setLaboratoryTestCost((long) costOfLaboratoryTest);
            newClaim.setSurgery(surgery);
            newClaim.setSurgeryCost((long) costOfSurgery);
            newClaim.setOtherItems(otherItems);
            newClaim.setOtherItemsCost((long) costOftherItems);
            newClaim.setNote(note);

            ClaimStatus claimStatus = null;
            for (ClaimStatus cs : claimStatusFacade.findAll()) {
                if (cs.getStatusId().intValue() == claimStatusId) {
                    claimStatus = cs;
                }
            }
            newClaim.setStatusId(claimStatus);

            Employee employee = null;
            for (Employee e : employeeFacade.findAll()) {
                if (e.getEmployeeId().intValue() == employeeId) {
                    employee = e;
                }
            }
            newClaim.setEmployeeId(employee);

            String requestResult;

            try {
                claimFacade.create(newClaim);
                requestResult = "The claim is sent successfully to the system.";
            } catch (Exception e) {
                requestResult = "Sending Error.";
                e.printStackTrace();
            }
            request.getSession().setAttribute("requestResult", requestResult);
            response.sendRedirect("claim.jsp");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
